#!/usr/bin/env python
# Create: 2018/10/16
__author__ = '749B'
"""登录Github"""

import requests
from bs4 import BeautifulSoup

from utils.base64p import b64decode_str

s = requests.Session()
r1 = s.get('https://github.com/login')
r1.encoding = r1.apparent_encoding
bs1 = BeautifulSoup(r1.text, features='html.parser')
form = bs1.find('form')
input_list = form.find_all('input')
data = {}
for input in input_list:
    name = input.attrs.get('name')
    value = input.get('value')  # 和上面的方法效果是一样的
    data[name] = value
# 不能把密码上传啊
with open('password/s3.txt') as f:
    auth = f.read()
    auth = auth.split('\n')
data['login'] = auth[0]
data['password'] = b64decode_str(auth[1])
r2 = s.post('https://github.com/session', data=data)
bs2 = BeautifulSoup(r2.text, features='html.parser')
title = bs2.find('title')
print(title)  # 登录成功返回的页面
r3 = s.get('https://github.com/settings/profile')
r3.encoding = r3.apparent_encoding  # 获取页面的编码，解决乱码问题
bs3 = BeautifulSoup(r3.text, features='html.parser')
title = bs3.find('title')
print(title)  # 用户信息页面的title
name = bs3.find('input', id="user_profile_name")
print(name.get('value'))  # 用户的 Name



